//
//  EditUserModel.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/1/31.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "EditUserModel.h"

@implementation EditUserModel

-(void)saveUserNickName:(NSString *)nickname andHearimage:(UIImage *)image AndPhoto:(NSArray *)photoarr AndDeletePhoto:(NSArray *)deletePhotp :(getData)hookData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:curUser.token forKey:@"token"];
    [param setObject:curUser.id forKey:@"uid"];
    [param setObject:@"ios" forKey:@"type"];
    [param setObject:[NSString stringWithFormat:@"%ld",(long)curUser.sex] forKey:@"sex"];
//    [param setObject:@"1" forKey:@"sex"];

    
    [MBProgressHUD showActivityMessageInView:ASLocalizedString(@"请稍后")];
//    [MBProgressHUD showInfoMessage:ASLocalizedString(@"请稍后")];
    
    if(StrValid(nickname))
    {
        [param setObject:nickname forKey:@"user_nickname"];
    }
    
    //判断是否有需要删除的视屏
//    if(ValidArray(deletePhotp))
//    {
//        NSString *delstr = @"";
//        for (NSString *str in deletePhotp) {
//            delstr = [NSString stringWithFormat:@"%@|%@",delstr,str];
//        }
//        [param setObject:delstr forKey:@"del_image"];
//    }
    
    BOOL nickHaveEdit = NO;
    
    //判断是否要上传头像
    if(image != nil)
    {
        [PPNetworkHelper uploadImagesWithURL:[cyurl makeURLWithC:@"user_api" A:@"update_user_info"] parameters:param name:@"avatar" images:@[image] fileNames:@[@"avatar.png"] imageScale:1 imageType:@"png" progress:^(NSProgress *progress) {
            
        } success:^(id responseObject) {
            [MBProgressHUD hideHUD];
            if([[responseObject valueForKey:@"code"] intValue] == 1)
            {
                curUser.avatar = [[responseObject valueForKey:@"data"] valueForKey:@"avatar"];
                if(nickHaveEdit == NO)//避免多次弹窗
                {
                    [MBProgressHUD showTopTipMessage:ASLocalizedString(@"资料已更新")];
                    [self updateUserinfo:^(BOOL success) {
                        KPostNotification(KNotificationLoginStateChange, @YES)
                    }];
                }
            }
            else
            {
                [MBProgressHUD showTopTipMessage:[responseObject valueForKey:@"msg"] isWindow:YES];
            }
            
            hookData(YES);
            
        } failure:^(NSError *error) {
            hookData(NO);
        }];
        
        nickHaveEdit = YES;
    }
    
    if(ValidArray(photoarr))
    {

        NSMutableArray *nameArr = [NSMutableArray array];
        for (int i=0; i<photoarr.count; i++) {
            [nameArr addObject:[NSString stringWithFormat:@"img%d",i]];
        }
        [PPNetworkHelper uploadImagesWithURL:[cyurl makeURLWithC:@"user_api" A:@"update_user_info"] parameters:param name:@"autoname" images:photoarr fileNames:nil imageScale:1 imageType:@"png" progress:^(NSProgress *progress) {
            
        } success:^(id responseObject) {
            [MBProgressHUD hideHUD];

            if([[responseObject valueForKey:@"code"] intValue] == 1)
            {
                [self updateUserinfo:^(BOOL success) {
                    
                }];
                if(nickHaveEdit == NO)//避免多次弹窗
                {
                    [MBProgressHUD showTopTipMessage:ASLocalizedString(@"资料已更新")];
                }
            }
            else
            {
                [MBProgressHUD showTipMessageInView:[responseObject valueForKey:@"msg"]];
            }
            hookData(YES);

            
        } failure:^(NSError *error) {
            hookData(NO);
        }];

        nickHaveEdit = YES;
    }
    
    if(nickHaveEdit == NO)
    {
        NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"update_user_info"];
        
        [CYNET POST:url parameters:param responseCache:^(id responseObject) {

        } success:^(id responseObject) {
            [MBProgressHUD hideHUD];

            [MBProgressHUD showTopTipMessage:ASLocalizedString(@"资料已更新")];
            [self updateUserinfo:^(BOOL success) {
                
            }];
            
            hookData(YES);
            
        } failure:^(NSString *error) {
            hookData(NO);
            [MBProgressHUD showTopTipMessage:error];
        } hasCache:NO];
    }
}

-(void)updateUserinfo:(getData)hookData
{
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"edit_user_info"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //[param setObject:@"" forKey:@""];
    
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        
//        if(self.getData)
//        {
            hookData(YES);
            UserInfo *model = [UserInfo modelWithDictionary:responseObject];
            curUser.user_nickname = model.user_nickname;
            curUser.avatar = model.avatar;
            curUser.sex = model.sex;
            curUser.img = model.img;
//            curUser.level = model.level;
            [userManager saveUserInfo];
//        }
    } failure:^(NSString *error) {
        hookData(YES);
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
}
@end
